package client.comm.commlib.comm_ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.R
import client.comm.commlib.base.BaseAdapter
import client.comm.commlib.comm_ui.bean.Image
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target

class ImageAdapter(context: Context, private val mImgLoader: RequestManager) : BaseAdapter<Image>(
    context,
    object : DiffUtil.ItemCallback<Image>() {
        override fun areItemsTheSame(oldItem: Image, newItem: Image): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Image, newItem: Image): Boolean {
            return oldItem == newItem
        }

    }
) {
    private var isSingleSelect = false
    var mListener: OnItemClickListener? = null
    fun setOnItemClickListener(mListener: OnItemClickListener?) {
        this.mListener = mListener
    }

    fun resetItem(items: List<Image?>?) {
        if (items != null) {
            this.submitList(items)
        }
    }

    fun setSingleSelect(singleSelect: Boolean) {
        isSingleSelect = singleSelect
    }

    fun updateItem(position: Int) {
        if (itemCount > position) {
            notifyItemChanged(position)
        }
    }

    override fun getItemViewType(position: Int): Int {
        val image = getItem(position)
        return if (image!!.id == 0L) 0 else 1
    }

    fun addItem(item: Image?) {
        if (item != null) {
//            this.mItems.add(item);
//            notifyItemChanged(mItems.size());
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 0) CamViewHolder(
            layoutInflater.inflate(
                R.layout.item_list_cam,
                parent,
                false
            )
        ) else

            return ImageViewHolder(layoutInflater.inflate(R.layout.item_list_image, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position)
        if (item!!.id != 0L) {
            val h = holder as ImageViewHolder
            h.mCheckView.isSelected = item.isSelect
            h.mMaskView.visibility = if (item.isSelect) View.VISIBLE else View.GONE

            // Show gif mask

            mImgLoader.load(item.imgUri)
                .apply(RequestOptions().error(R.mipmap.ic_default_image).centerCrop())
                .into(h.mImageView)
            h.mCheckView.visibility = if (isSingleSelect) View.GONE else View.VISIBLE
        }
        holder.itemView.setOnClickListener {
            if (mListener != null) {
                mListener!!.onItemClick(position)
            }
        }
    }

    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
        if (holder is ImageViewHolder) {
            mImgLoader.clear(holder.mImageView)
        }
    }

    private class CamViewHolder internal constructor(itemView: View?) : RecyclerView.ViewHolder(
        itemView!!
    )

    private class ImageViewHolder internal constructor(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var mImageView: ImageView
        var mCheckView: ImageView
        var mMaskView: View

        init {
            mImageView = itemView.findViewById<View>(R.id.iv_image) as ImageView
            mCheckView = itemView.findViewById<View>(R.id.cb_selected) as ImageView
            mMaskView = itemView.findViewById(R.id.lay_mask)
        }
    }

    interface OnItemClickListener {
        fun onItemClick(pos: Int)
    }
}

